<?php

set_time_limit(1800);
error_reporting(E_ERROR);
$city = get_city();

$filename = "city.php";
file_exists($filename) or touch($filename);
file_put_contents($filename, var_export($citys,true));


function get_city(){
	$url = 'http://flash.weather.com.cn/wmaps/xml/china.xml';
	
	$result = GetData($url);
	
	$getObj = simplexml_load_string($result);
	
	$city = array();
	
	foreach($getObj->city as $province){
		$pro = $province->attributes();
		
		$proUrl = 'http://flash.weather.com.cn/wmaps/xml/'.$pro['pyName'].'.xml';
		
		$pro_result = GetData($proUrl);
		
		
		try{
			$cityObj = simplexml_load_string($pro_result);
		}catch(Exception $ex){
			continue;
		}
		
		foreach($cityObj->city as $city1){
			$ct = $city1->attributes();
			if($ct['cityname']=='市中心'){
				$city[(string)$pro['cityname']] = (string)$ct['url'];
			}else{
				$city[(string)$ct['cityname']] = (string)$ct['url'];
			}
			
			$cityUrl = 'http://flash.weather.com.cn/wmaps/xml/'.$ct['pyName'].'.xml';
			
			$city_result = GetData($cityUrl);
			
			try
			{
				$cityObj2 = simplexml_load_string($city_result);
				
			}catch (Exception $e){
				continue;
			}
			foreach($cityObj2->city as $city2){
				$ct2 = $city2->attributes();
				$city[(string)$ct2['cityname']] = (string)$ct2['url'];
			}
		}
	}
	return $city;
}



function GetData($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
	
	
}


echo 'test';
?>